package com.ouman.luoliluoli.utils;

import android.content.Context;
import android.util.Log;

import com.ouman.luoliluoli.models.DaysModel;
import com.ouman.luoliluoli.models.UserInfo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class LocalSaveHelper {
    private String daysDataFileName = "daysDataArray";

    // write any object to internal storage
    public void saveToInternalStorage(Object obj, String storage_filename, Context context, int mode) {
        try {
            FileOutputStream fos = context.openFileOutput(storage_filename, mode);
            ObjectOutputStream of = new ObjectOutputStream(fos);
            of.writeObject(obj);
            of.flush();
            of.close();
            fos.close();
        }
        catch (Exception e) {
            Log.e("InternalStorage", e.getMessage());
        }
    }


    // read array list from internal storage
    @SuppressWarnings("unchecked")
    private ArrayList<DaysModel> readDaysDataFromInternalStorage(Context context, String fileName) throws IOException {
        ArrayList<DaysModel> toReturn = null;
        try {
            FileInputStream fis = context.openFileInput(fileName);
            ObjectInputStream oi = new ObjectInputStream(fis);
            toReturn = (ArrayList<DaysModel>) oi.readObject();
            oi.close();
        } catch (ClassNotFoundException e) {
            Log.e("InternalStorage", e.getMessage());
        }
        return toReturn;
    }

    // 删除daysArrayList的元素
    public void removeDaysDataArrayItem(Context context, int position){
        ArrayList<DaysModel> daysArrayList;
        try{
            System.out.println("开始删除array list元素");
            daysArrayList = readDaysDataFromInternalStorage(context, daysDataFileName);
            daysArrayList.remove(position);
            saveToInternalStorage(daysArrayList, daysDataFileName, context, Context.MODE_PRIVATE);
        } catch (IOException e) {
            System.out.println("=====删除days数据出错");
        }
    }
    // 更新本地days数据
    public void updateDaysDataArrayItem(Context context, int position, DaysModel newDayModel){
        ArrayList<DaysModel> daysArrayList;
        try{
            System.out.println("开始更新days元素");
            daysArrayList = readDaysDataFromInternalStorage(context, daysDataFileName);
            daysArrayList.set(position, newDayModel);
            saveToInternalStorage(daysArrayList, daysDataFileName, context, Context.MODE_PRIVATE);
        } catch (IOException e) {
            System.out.println("=====更新出错");
        }
    }
    // 编辑daysArrayList元素
    public void editDaysDataArrayItem(Context context, int position, DaysModel new_day){
        ArrayList<DaysModel> daysArrayList;
        try{
            System.out.println("开始编辑array list元素");
            daysArrayList = readDaysDataFromInternalStorage(context, daysDataFileName);
            daysArrayList.set(position, new_day);
            saveToInternalStorage(daysArrayList, daysDataFileName, context, Context.MODE_PRIVATE);
        } catch (IOException e) {
            System.out.println("=====编辑days数据出错");

        }
    }


    @SuppressWarnings("unchecked")
    public UserInfo readUserInfoFromInternalStorage(Context context, String fileName) throws IOException {
        UserInfo toReturn = null;
        try {
            FileInputStream fis = context.openFileInput(fileName);
            ObjectInputStream oi = new ObjectInputStream(fis);
            toReturn = (UserInfo) oi.readObject();
            oi.close();
        } catch (ClassNotFoundException e) {
            Log.e("InternalStorage", e.getMessage());
        }
        return toReturn;
    }


}
